package com.coffee.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coffee.framework.common.annotation.AutoLog;
import com.coffee.framework.common.result.R;
import com.coffee.system.common.dto.SysRoleAddDTO;
import com.coffee.system.common.dto.SysRoleAssignMenuDTO;
import com.coffee.system.common.dto.SysRoleEditDTO;
import com.coffee.system.common.dto.SysRoleQueryDTO;
import com.coffee.system.common.vo.SysDeptVO;
import com.coffee.system.common.vo.SysMenuVO;
import com.coffee.system.common.vo.SysRoleListVO;
import com.coffee.system.common.vo.SysRoleVO;
import com.coffee.system.service.ISysRoleService;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author Kevin
 * @since 2021-06-10
 */
@RestController
@RequestMapping("/system/sysRole")
@AllArgsConstructor
public class SysRoleController {

    private ISysRoleService sysRoleService;

    /**
     * 分页查询
     */
    @GetMapping("/page")
    @SaCheckPermission("system:sysRole:page")
    @AutoLog(module = "角色管理", title = "分页查询")
    public R<Page<SysRoleListVO>> page(Page reqPage, SysRoleQueryDTO req) {
        return R.success(sysRoleService.page(reqPage, req));
    }

    /**
     * 新增
     */
    @PostMapping("/add")
    @SaCheckPermission("system:sysRole:add")
    @AutoLog(module = "角色管理", title = "新增")
    public R<Void> add(@Validated @RequestBody SysRoleAddDTO req) {
        sysRoleService.add(req);
        return R.success();
    }

    /**
     * 修改
     */
    @PostMapping("/edit")
    @SaCheckPermission("system:sysRole:edit")
    @AutoLog(module = "角色管理", title = "修改")
    public R<Void> edit(@Validated @RequestBody SysRoleEditDTO req) {
        sysRoleService.edit(req);
        return R.success();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @SaCheckPermission("system:sysRole:remove")
    @AutoLog(module = "角色管理", title = "删除")
    public R<Void> remove(@RequestParam String ids) {
        sysRoleService.remove(ids);
        return R.success();
    }

    /**
     * 查看
     */
    @GetMapping("/view")
    @SaCheckPermission("system:sysRole:view")
    @AutoLog(module = "角色管理", title = "查看")
    public R<SysRoleVO> view(@RequestParam String id) {
        return R.success(sysRoleService.view(id));
    }

    @GetMapping("/export")
    @SaCheckPermission("system:sysRole:export")
    @AutoLog(module = "角色管理", title = "导出")
    @SneakyThrows
    public void export(SysRoleQueryDTO req, HttpServletResponse response) {
        String filename = "角色列表" + "_" + System.currentTimeMillis() + ".xlsx";
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1));
        EasyExcel.write(response.getOutputStream())
                .head(SysRoleListVO.class)
                .sheet("角色列表")
                .doWrite(sysRoleService.list(req));
    }

    /**
     * 查询下拉框列表
     */
    @GetMapping("/listOptions")
    @AutoLog(module = "角色管理", title = "查询下拉框列表")
    public R<List<SysRoleVO>> listOptions() {
        return R.success(sysRoleService.listOptions());
    }

    /**
     * 分配菜单
     */
    @PostMapping("/assignMenu")
    @SaCheckPermission("system:sysRole:assignMenu")
    @AutoLog(module = "角色管理", title = "分配菜单")
    public R<Void> assignMenu(@Validated @RequestBody SysRoleAssignMenuDTO req) {
        sysRoleService.assignMenu(req);
        return R.success();
    }

    /**
     * 查询角色关联的菜单
     */
    @GetMapping("/listRoleMenus")
    @AutoLog(module = "角色管理", title = "查询角色关联的菜单")
    public R<List<SysMenuVO>> listRoleMenus(@RequestParam String roleId) {
        return R.success(sysRoleService.listRoleMenus(roleId));
    }

    /**
     * 查询角色关联的部门
     */
    @GetMapping("/listRoleDepts")
    @AutoLog(module = "角色管理", title = "查询角色关联的部门")
    public R<List<SysDeptVO>> listRoleDepts(@RequestParam String roleId) {
        return R.success(sysRoleService.listRoleDepts(roleId));
    }

    /**
     * 下拉列表
     */
    @GetMapping("/selectOptions")
    @AutoLog(module = "角色管理", title = "下拉列表")
    public R<List<SysRoleVO>> selectOptions() {
        return R.success(sysRoleService.selectOptions());
    }

}

